package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.swust.model.ClassifyBean;
import cn.swust.service.ClassifyService;
import cn.swust.util.DBUtil;

public class ClassifyServiceImpl implements ClassifyService {

	public int addClassify(ClassifyBean classify) {
		int ret = 0;
		try{
		String insertSql = "insert into classify(class_caption)" +
		" values ('" + classify.getCaption() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delClassify(int id) {
		String delSql = "delete from classify where class_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int modClassify(int id, ClassifyBean classify) {
		String modSql = "update classify set class_caption = '" + classify.getCaption() + "' where class_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public ClassifyBean queryById(int id) {
		String querSql = "select * from classify where class_id = " + id;
		ClassifyBean classify = new ClassifyBean();
		ResultSet rs = DBUtil.querySql(querSql, DBUtil.creatStatement(DBUtil.getConnection()));
		try {
			while(rs.next()){
				classify.setId(rs.getInt("class_id"));
				classify.setCaption(rs.getString("class_caption"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return classify;
	}

}
